package com.idvert.smsplugin;

import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class SmsService extends Service {

    final String TAG = getClass().getSimpleName();
    private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";

    private SMSBroadcastReceiver smsBroadcastReceiver;

    public SmsService() {
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "Start SMS Servic.");

        smsBroadcastReceiver = new SMSBroadcastReceiver();
        smsBroadcastReceiver.setMessageListener(new SMSBroadcastReceiver.MessageListener() {
            @Override
            public void onReceive(PhoneMessage message) {
                Log.d(TAG, "--->发送SMS:" + message);
            }
        });

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        registerReceiver();

        return mBinder;
//        throw new UnsupportedOperationException("Not yet implemented");
    }

    void registerReceiver() {
        IntentFilter intentFilter = new IntentFilter(ACTION);
        intentFilter.setPriority(1000);

        registerReceiver(smsBroadcastReceiver, intentFilter);
    }

    public class LocalBinder extends Binder {

        SmsService getService() {
            Log.i("TAG", "getService ---> " + SmsService.this);
            return SmsService.this;
        }

    }

    private final IBinder mBinder = new LocalBinder();

}
